package com.caigou.comm.base.widget

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.text.TextUtils
import android.view.*
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import com.caigou.comm.R
import com.caigou.comm.base.bean.AppBackgroundInfo
import com.caigou.comm.base.ext.getScreenWidth
import com.caigou.comm.base.utils.ImageLoader
import com.google.android.material.tabs.TabLayout

/**
 * @author #Suyghur.
 * Created on 2021/12/14
 */
class RewardDialog(context: Context, private val headImg: String, private val callback: IRewardClickCallback) : Dialog(context) {

    private lateinit var viewPager: ViewPager
    private lateinit var tabLayout: TabLayout
    private lateinit var pagerAdapter: RewardPageAdapter
    private lateinit var moneyContainer: View
    private lateinit var flowerContainer: View

    private lateinit var ivClose: ImageView

    lateinit var tvMoney: TextView
    lateinit var tvFlower: TextView

    private lateinit var etMoney: EditText
    private lateinit var etFlower: EditText

    private lateinit var btnReward: Button
    private lateinit var btnMoneyCharge: Button
    private lateinit var btnFlowerCharge: Button

    lateinit var ivAvatar: ImageView

    init {
        setCanceledOnTouchOutside(true)
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        initView(context)
    }

    private fun initView(context: Context) {
        setContentView(R.layout.comm_reward_dialog)
        val attr = window?.attributes as WindowManager.LayoutParams
        //设置dialog在布局中的位置
        attr.gravity = Gravity.BOTTOM
        attr.width = getScreenWidth(context)

        viewPager = findViewById(R.id.reward_dialog_view_pager)
        tabLayout = findViewById(R.id.reward_dialog_tab_layout)
        moneyContainer = View.inflate(context, R.layout.comm_reward_money, null)
        flowerContainer = View.inflate(context, R.layout.comm_reward_flower, null)

        tvMoney = moneyContainer.findViewById(R.id.reward_money_tv_num)
        tvMoney.text = AppBackgroundInfo.instance.userBean!!.balance.toString()

        tvFlower = flowerContainer.findViewById(R.id.reward_flower_tv_num)
        tvFlower.text = AppBackgroundInfo.instance.userBean!!.flower.toString()

        etMoney = moneyContainer.findViewById(R.id.reward_money_et_num)
        etFlower = flowerContainer.findViewById(R.id.reward_flower_et_num)

        btnReward = findViewById(R.id.reward_dialog_btn_reward)
        btnMoneyCharge = moneyContainer.findViewById(R.id.reward_money_btn_charge)
        btnFlowerCharge = flowerContainer.findViewById(R.id.reward_flower_btn_charge)

        ivAvatar = findViewById(R.id.reward_dialog_iv_avatar)
        if (TextUtils.isEmpty(headImg)) {
            ivAvatar.setImageResource(R.drawable.img_comm_avatar)
        } else {
            ImageLoader.loadCircle(context, headImg, ivAvatar)
        }

        val views = mutableListOf(
            moneyContainer,
            flowerContainer
        )
        val titles = mutableListOf(
            "红包打赏",
            "赠送小红花"
        )

        pagerAdapter = RewardPageAdapter(views, titles)

        viewPager.adapter = pagerAdapter
        tabLayout.setupWithViewPager(viewPager)
        tabLayout.setTabsFromPagerAdapter(pagerAdapter)

        btnMoneyCharge.setOnClickListener {
            callback.onChargeMoney()
        }

        btnFlowerCharge.setOnClickListener {
            callback.onChargeFlower()
        }

        btnReward.setOnClickListener {
            val type = if (viewPager.currentItem == 0) {
                1
            } else {
                2
            }
            val num = if (viewPager.currentItem == 0) {
                etMoney.text.toString().toInt()
            } else {
                etFlower.text.toString().toInt()
            }
            callback.onReward(type, num)
        }

        ivClose = findViewById(R.id.reward_dialog_iv_close)
        ivClose.setOnClickListener {
            dismiss()
        }
    }


    class RewardPageAdapter(private val views: MutableList<View>, private val titles: MutableList<String>) : PagerAdapter() {
        override fun getCount(): Int {
            return views.size
        }

        override fun getPageTitle(position: Int): CharSequence {
            return titles[position]
        }

        override fun isViewFromObject(view: View, `object`: Any): Boolean {
            return view == `object`
        }

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            val view = views[position]
            container.addView(view)
            return view
        }

        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
            container.removeView(`object` as View)
        }
    }

    interface IRewardClickCallback {
        fun onChargeMoney()

        fun onChargeFlower()

        fun onReward(type: Int, num: Int)
    }
}

